/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.gui.widgets;

import java.util.LinkedList;
import java.util.List;

import javax.swing.event.EventListenerList;

import net.miblounge.gradecalculator.core.descriptors.modulelistitems.SelectableModuleLists;
import net.miblounge.gradecalculator.core.descriptors.modulelists.ModuleList;
import net.miblounge.gradecalculator.gui.interfaces.ISelectableModuleListsSelectionListener;
import net.miblounge.gradecalculator.guicommon.widgets.MultiComboBox;

import com.trolltech.qt.gui.QWidget;

public class ModuleListsSelector extends MultiComboBox
{
    private final String baseId;

    private final EventListenerList listeners = new EventListenerList();

    public ModuleListsSelector(final QWidget parent, final SelectableModuleLists lists)
    {
        super(parent, getValueLists(lists), lists.getPickCount());
        baseId = lists.getIdentifier() + ".";
    }

    private static List<String> getValueLists(final SelectableModuleLists lists)
    {
        final List<String> result = new LinkedList<String>();
        for (final ModuleList type : lists.getModuleLists()) {
            result.add(type.getName());
        }
        return result;
    }

    protected synchronized void notifySelectionChanged(final String identifier, final boolean selected)
    {
        for (final ISelectableModuleListsSelectionListener l : this.listeners
                .getListeners(ISelectableModuleListsSelectionListener.class)) {
            l.selectionChanged(identifier, selected);
        }
    }

    public void addSelectionListener(final ISelectableModuleListsSelectionListener listener)
    {
        this.listeners.add(ISelectableModuleListsSelectionListener.class, listener);
    }

    @Override
    protected void selectionChangedEvent(final String item, final boolean isSelected)
    {
        notifySelectionChanged(baseId + item, isSelected);
    }

    public void setSelectionEnabled(final boolean isEnabled)
    {}

}